package br.com.cqipac.dao;

// Generated 31/03/2008 01:38:36 by Hibernate Tools 3.2.1.GA

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.com.cqipac.to.Funcionario;
import br.com.cqipac.to.Setor;

/**
 * Home object for domain model class Setor.
 * 
 * @see br.com.cqipac.to.Setor
 * @author Hibernate Tools
 */
@Stateless
public class SetorDao implements RemoteDao {

	private static final Log log = LogFactory.getLog(SetorDao.class);

	@PersistenceContext
	// (unitName = "CQIpacEJB")
	private EntityManager entityManager;

	public void persist(Object transientInstance) throws Exception {
		log.debug("persisting Setor instance");

		entityManager.persist(transientInstance);
		log.debug("persist successful");

	}

	public void remove(Object persistentInstance) throws Exception {
		
		
		persistentInstance = this.findById(((Setor) persistentInstance)
				.getCodSetor());

			log.debug("removing Funcionario instance");
			entityManager.remove((Setor) persistentInstance);
			log.debug("remove successful");
			
			
//		log.debug("removing Setor instance");
//		entityManager.remove((Setor) persistentInstance);
//		log.debug("remove successful");

	}

	public Setor merge(Object detachedInstance) throws Exception {
		log.debug("merging Setor instance");

		Setor result = entityManager.merge((Setor) detachedInstance);
		log.debug("merge successful");
		return result;

	}

	public Setor findById(Integer id) throws Exception {
		log.debug("getting Setor instance with id: " + id);

		Setor instance = entityManager.find(Setor.class, id);
		log.debug("get successful");
		return instance;

	}

	public List<Setor> list() throws Exception {

		log.debug("getting Setor instances list");

		List<Setor> instance = entityManager.createQuery(
				"from Setor s where s.stsSetor = true").getResultList();
		log.debug("get successful");
		return instance;

	}

	@Override
	public Object find(Object searchingIsntance, Integer intTipoBusca)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Setor> list(Object searchingInstance, Integer intTipoBusca)
			throws Exception {

		Setor setor = (Setor) searchingInstance;

		log.debug("getting Setor instances list");
		String query = "from Setor s where 1=1";

		if (setor.getDesSetor() != null) {
			query += " and s.desSetor LIKE '"
					+ ((Setor) searchingInstance).getDesSetor() + "%' ";
		}
		if (setor.getNomSetor() != null) {
			query += " and s.nomSetor LIKE '"
					+ ((Setor) searchingInstance).getNomSetor() + "%' ";
		}

		List<Setor> instance = entityManager.createQuery(query).getResultList();
		log.debug("get successful");
		return instance;

	}
}
